package com.sh.wy.fee.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sh.util.Result;
import com.sh.wy.fee.entity.FeePower;
import com.sh.wy.fee.entity.FeePowerParam;
import com.sh.wy.fee.service.IFeePowerService;
import com.sh.wy.system.entity.LiveHouse;
import com.sh.wy.system.service.ILiveHouseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 电费表 前端控制器
 * </p>
 *
 * @author 世豪学Java
 * @since 2023-12-23
 */
@RestController
@RequestMapping("/api/feePower")
public class FeePowerController {

    @Autowired
    private IFeePowerService feePowerService;

    @Autowired
    private ILiveHouseService liveHouseService;

    @ApiOperation("电费列表")
    @GetMapping("/list")
    public Result getList(FeePowerParam param) {
        IPage<FeePower> page = feePowerService.getList(param);
        return Result.success(page);
    }

    @ApiOperation("新增电费")
    @PostMapping
    public Result addFeePower(@RequestBody FeePower feePower) {
        //查询该房间是否没有人使用
        QueryWrapper<LiveHouse> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(LiveHouse::getHouseId, feePower.getHouseId())
                .eq(LiveHouse::getUseStatus, 1);
        LiveHouse liveHouse = liveHouseService.getOne(wrapper);
        if (Objects.isNull(liveHouse)) {
            return Result.error(500, "该房间还没人使用");
        }

        //有人使用的情况下
        //把查询到的业主id赋值给feePower
        feePower.setUserId(liveHouse.getUserId());
        feePowerService.saveFeePower(feePower);
        return Result.success();
    }

    @ApiOperation("编辑电费")
    @PutMapping
    public Result editFeePower(@RequestBody FeePower feePower){
        //查询该房间是否没有人使用
        QueryWrapper<LiveHouse> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(LiveHouse::getHouseId, feePower.getHouseId())
                .eq(LiveHouse::getUseStatus, 1);
        LiveHouse liveHouse = liveHouseService.getOne(wrapper);
        if (Objects.isNull(liveHouse)) {
            return Result.error(500, "该房间还没人使用");
        }
        //把查询出来的用户id设置到电费实体里面
        feePower.setUserId(liveHouse.getUserId());
        feePowerService.updateFeePower(feePower);
        return Result.success();
    }

    @ApiOperation("删除电费")
    @DeleteMapping("/{powerId}")
    public Result deleteFeePower(@PathVariable("powerId")Integer powerId){
        //如果已经缴费，不能删除
        QueryWrapper<FeePower> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(FeePower::getPowerId,powerId)
                .eq(FeePower::getPayPowerStatus,1);
        FeePower one = feePowerService.getOne(wrapper);
        if (one != null){
            return Result.error(500,"删除失败，已经缴费");
        }
        //删除操作
        boolean remove = feePowerService.removeById(powerId);
        if (remove){
            return Result.success();
        }
        return Result.error();
    }

    //本质就是修改数据
    @ApiOperation("缴费")
    @PostMapping("/payPower")
    public Result payPower(@RequestBody FeePower feePower){
        boolean update = feePowerService.updateById(feePower);
        if (update){
            return Result.success();
        }
        return Result.error();
    }

}
